package cn.edu.neusoft.amicooldemo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.amicooldemo.bean.Constants;
import cn.edu.neusoft.amicooldemo.bean.LoginBean;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import cn.edu.neusoft.amicooldemo.service.UserService;

public class LoginActivity extends AppCompatActivity {
    private EditText et_userName;
    private EditText et_password;

    private TextView login;
    private TextView reg;
    private String userName;
    private String password;

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//
//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });

        sp = getSharedPreferences("amicool_user", Context.MODE_PRIVATE);
        if (sp.getString("SessionID", "") != "") {
            userName = sp.getString("username", "");
            password = sp.getString("password", "");
            Retrofit retrofit =
                    new Retrofit.Builder().baseUrl(Constants.SERVERADDRESS)
                            .addConverterFactory(GsonConverterFactory.create()).build();
            UserService service = retrofit.create(UserService.class);
            Call<LoginBean> call = service.login(userName, password);
            call.enqueue(new Callback<LoginBean>() {
                @Override
                public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                    LoginBean result = response.body();
                    editor = sp.edit();
                    editor.putString("SessionID", result.getSessionid());
                    editor.commit();
                    Intent intent = new Intent(LoginActivity.this,
                            MainActivity.class);
                    LoginActivity.this.startActivity(intent);
                    finish();
                }

                @Override
                public void onFailure(Call<LoginBean> call, Throwable t) {
                    Toast.makeText(LoginActivity.this, "fail" + t.toString(), Toast.LENGTH_SHORT).show();
                }
            });
            //startActivity(new Intent(LoginActivity.this,MainActivity.class));

        }

        et_userName = (EditText) findViewById(R.id.username);
        et_password = (EditText) findViewById(R.id.password);
        et_userName.setText(userName);
        login = (TextView) findViewById(R.id.login_btn);
        reg = (TextView) findViewById(R.id.reg_btn);
        reg.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                userName = et_userName.getText().toString();
                password = et_password.getText().toString();

                if ("".equals(userName)) {
                    Toast.makeText(LoginActivity.this, "账号不能为空", Toast.LENGTH_SHORT).show();
                } else if ("".equals(password)) {
                    Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    Retrofit retrofit =
                            new Retrofit.Builder().baseUrl(Constants.SERVERADDRESS)
                                    .addConverterFactory(GsonConverterFactory.create()).build();
                    UserService service = retrofit.create(UserService.class);
                    Call<LoginBean> call = service.login(userName, password);
                    call.enqueue(new Callback<LoginBean>() {
                        @Override
                        public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                            LoginBean result = response.body();
                            if (result.getSessionid() != null) {

                                Intent intent = new Intent(LoginActivity.this,
                                        MainActivity.class);


                                editor = sp.edit();
                                editor.putString("SessionID", result.getSessionid());
                                editor.putInt("myid", result.getId());
                                editor.putString("username", userName);
                                editor.putString("password", password);
                                editor.commit();
                                LoginActivity.this.startActivity(intent);
                                finish();
                            } else {
                                Toast.makeText(LoginActivity.this, "账号信息不正确", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<LoginBean> call, Throwable t) {
                            Toast.makeText(LoginActivity.this, "fail" + t.toString(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }
}
